package cn.gdcp.listview;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener {
    private ListView nameListView;
    private String[] nameArr = {"张三","李四","王五","赵六","钱七"};
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private MyAdapter adapter;
    private Button btnAddStu;

    private EditText edtKeyword;
    private Button btnSeach;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStuList();

        initAdapter();

        initListView();

        initAddBtn();

        initSearchView();
    }

    private void initSearchView() {
        edtKeyword = findViewById(R.id.edt_keyword);
        btnSeach = findViewById(R.id.btn_search);

        btnSeach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyword = edtKeyword.getText().toString().trim();
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
            }
        });
    }

    private void search(ArrayList<Student> studentArrayList, String keyword) {
        searchResultList.clear();
        for(int i = 0; i<studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);

            if (s.getName().contains(keyword)){
                searchResultList.add(s);
            }
        }
    }

    private void initAddBtn() {
        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
    }

    private void initListView() {
        nameListView = findViewById(R.id.name_listview);

        nameListView.setAdapter(adapter);

        nameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student s = studentArrayList.get(i);
                Toast.makeText(MainActivity.this,
                        "姓名"+s.getName() + "年龄"
                                + s.getAge(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                intent.putExtra("NAME", s.getName());
                intent.putExtra("AGE", s.getAge());
                intent.putExtra("IMG", s.getImgId());
                intent.putExtra("NO", s.getNo());
                startActivityForResult(intent,1002);
            }
        });
    }

    private void initAdapter() {
        adapter = new MyAdapter(MainActivity.this, studentArrayList,MainActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.e("MainActivity","requestCode = "+ requestCode + "resultCode = " + resultCode);

        if(data == null){
            return;
        }

        if(resultCode == 2001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);

            Student student = new Student("18006", imgId, name, age);
            studentArrayList.add(student);
            adapter.notifyDataSetChanged();
        }
        else if(resultCode == 3001){
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);
            String no = data.getStringExtra("NO");

            for(int i = 0; i<studentArrayList.size(); i++){
                Student s = studentArrayList.get(i);

                if(s.getNo().equals(no)){
                    s.setName(name);
                    s.setAge(age);
                    s.setImgId(imgId);
                }
            }


        }
    }

    private void initStuList() {
        Student s1 = new Student("18001", R.drawable.dog1, "张三", 18);
        Student s2 = new Student("18002", R.drawable.dog2, "李四", 18);
        Student s3 = new Student("18003", R.drawable.dog3, "王五", 18);
        Student s4 = new Student("18004", R.drawable.dog4, "赵六", 18);
        Student s5 = new Student("18005", R.drawable.dog5, "钱七", 18);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    @Override
    public void del(int i) {
        Log.e("MainActivity", "del" + i);
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
